package io.adrainty.bolee.framework.commons.utils;

import io.adrainty.bolee.framework.commons.basic.ResponseCode;
import io.adrainty.bolee.framework.commons.basic.ResultDTO;
import io.adrainty.bolee.framework.commons.exception.AppException;

/**
 * @author AdRainty
 * @version V1.0.0
 * @since 2025/7/28 上午1:09
 */

@SuppressWarnings("unused")
public class ResultDTOUtil {

    private ResultDTOUtil() {}

    public static <T> ResultDTO<T> success(T data) {
        return ResultDTO.<T>builder()
                .code(Integer.parseInt(ResponseCode.SUCCESS.getCode()))
                .msg(ResponseCode.SUCCESS.getDesc())
                .data(data)
                .success(true)
                .build();
    }

    public static <T> ResultDTO<T> error(String message) {
        return ResultDTO.<T>builder()
                .code(Integer.parseInt(ResponseCode.UN_ERROR.getCode()))
                .msg(message)
                .success(false)
                .build();
    }

    public static <T> ResultDTO<T> error(ResponseCode responseCode) {
        return ResultDTO.<T>builder()
                .code(Integer.parseInt(responseCode.getCode()))
                .msg(responseCode.getDesc())
                .success(false)
                .build();
    }

    public static <T> ResultDTO<T> error(int code, String message) {
        return ResultDTO.<T>builder()
                .code(code)
                .msg(message)
                .success(false)
                .build();
    }

    public static <T> ResultDTO<T> error(int code, String message, T data) {
        return ResultDTO.<T>builder()
                .code(code)
                .msg(message)
                .data(data)
                .success(false)
                .build();
    }

    public static <T> void assertResultSuccess(ResultDTO<T> result) {
        if (!Boolean.TRUE.equals(result.getSuccess())) {
            throw new AppException(result.getCode(), result.getMsg());
        }
    }

    public static <T> T assertResultDataNotNull(ResultDTO<T> result) {
        assertResultSuccess(result);
        if (result.getData() == null) {
            throw new AppException(ResponseCode.UN_ERROR);
        }
        return result.getData();
    }

}
